<template>
  <div style="text-align:right">
    <!-- <el-badge :value=n class="item">
      <router-link 
        to="/car" >
        <img src="../assets/images/shopcar.png" alt="">
        </router-link>
    </el-badge> -->
    <el-button type="primary" 
      style="margin-right:100px;"
      @click="goToCar()">
      添加购物车</el-button>
    <!-- <el-button type="primary"
      @click="buy()">
      点击购买</el-button> -->
  </div>
</template>
<script>
export default {
  name:'buyCompt',
  props:['data'],
  data(){
    return{
      
    }
  },
  mounted(){
    //console.log(this.data);
    
  },
  methods:{
    goToCar(id){
      //console.log(this.data);
      for (const key in this.data) {
        if (this.data.hasOwnProperty(key)) {
          if (this.data[key]=='') {
            this.$message({
              type:'error',
              message:'请选择属性'
            })
            return
          }
        }
      }
      this.GLOBAL.tokenRequest({
          method:"post",
          baseURL:this.GLOBAL.RNTY_SHOP_MALL_URL,
          url:"carts/insert",
          data:this.data
        }).then(({data:data})=>{
          // console.log(data);
          if (data.result==2) {
            this.$message({
              message: data.message,
              type: 'error'
            })
            setTimeout(()=>this.GLOBAL.authLogin('user'),500)
            return
          }
          if(data.result){
            //成功
            this.$message({
              message: '添加购物车成功',
              type: 'success'
            });
            setTimeout(()=>this.$router.push({path:this.data.sku?'/shoplist':'/classlist'}),500)
          }else{
            this.$message({
              message: data.message,
              type: 'error'
            })
          }
        })
      // console.log('添加购物车')
    },
    buy(){
      // console.log('立即购买')
      this.$router.push({path:'/order'})
    }
  }
}
</script>


